const config = window.a2dmah5 && window.a2dmah5.config ? window.a2dmah5.config : {};
const { appPath, appId, appName, logoUrl } = config;

const _ServiceConfig = {
    ApiService: getApiServiceUrl,
    WebService: getWebServiceUrl,
    WebRootPath: getWebRootPath,
    EntityFormService: getApiServiceUrl
};

const EnvConfig = {
    getServiceUrl,
    setEnv,
    env: null,
    isProd: false,
    isPreview: !appPath,
    appName,
    logoUrl,
    appId: appId || 'ab88cfef-b1f7-4f2f-8da7-bfd9d9398ecc',
    prodPath: '/admin',
    tokenKey: config.tokenKey || 'D145C602-0B96-4684-A8A1-B34C38E3926D',
    userIdKey: config.userIdKey || '5FA9E4D4-3A13-468E-918D-F9750840F763',
    userInfoKey: config.wxUserKey || 'F29DD7CC-F5FE-4C28-A705-2E7D6F525070'
};

// ctx koa对象，ctx不为空表示是服务器
function setEnv() {
    EnvConfig.env = getWebEnv();
    EnvConfig.isProd = EnvConfig.env === "prd";
}

function getWebEnv() {
    const h = window.location.hostname.toLowerCase();
    return getEnv(h);
}

function getEnv(h) {
    if (h.indexOf("localhost") >= 0 || h.indexOf('192.168.1.166') >= 0) return "local";
    return "prd";
}

function getWebServiceUrl() {
    return EnvConfig.isProd ? EnvConfig.prodPath + "/" : "http://app3.a2china.cn/web/";
}

function getWebRootPath() {
    return EnvConfig.isProd ? EnvConfig.prodPath : '';
}

function getApiServiceUrl() {
    return EnvConfig.isProd ? '/api/' : '/api/';
}

function getServiceUrl(serverName) {
    if (!_ServiceConfig[serverName]) serverName = 'ApiService';
    return () => _ServiceConfig[serverName]();
}

EnvConfig.setEnv();

export default EnvConfig;